SCE Digital Image Processing 2 - 20.12.2020

Benny Aman 205873599

עד מתי להגיש את העבודה :

You have to post it to the Moodle Box which is going to be opened in the Moodle Adress of the Course before 31.12.2020.
Enjoy your work

How to send your work

Next bohan

Aim of the Assignment

This assignment comprised two parts:

Part 1: exercise on Chaps 3 and 4 - Filtering in Frequency Domain

  1. Remove the noise from the input images:
2. Submit your code and the output images.

נשתמש במסנן מסוג מדיאן שלפי ההרצאה הוא הטוב ביותר עבור סינון רעשים

a = imread("Q3_1_1.tif");
figure,imshow(a),title(' Original Q3_1_1 Image')
A = medfilt2(a);
Min_A = min(min(A));
Max_A = max(max(A));
figure,imshow(A,[Min_A Max_A]),title('Image Q3_1_1 after Median Filter')
imwrite(A,'Q3_1_1_Median.tif');
b = imread('Q3_1_2.tif');
figure,imshow(b),title('Original Q3_1_2 Image')
B = medfilt2(b);
Min_B = min(min(B));
Max_B = max(max(B));
figure,imshow(B,[Min_B Max_B]),title('Image Q3_1_2 after Median Filter')
imwrite(B,'Q3_1_2_Median.tif');
c = imread('Q3_1_3.tif');
figure,imshow(c),title('Original Q3_1_3 Image')
C = medfilt2(c);
Min_C = min(min(C));
Max_C = max(max(C));
figure,imshow(C,[Min_C Max_C]),title('Image Q3_1_3 after Median Filter')
imwrite(C,'Q3_1_3_Median.tif');
d = imread("Q3_1_4.tif");
figure,imshow(d),title('Original Q3_1_4 Image')
D = medfilt2(d);
Min_D = min(min(D));
Max_D = max(max(D));
figure,imshow(D,[Min_D Max_D]),title('Image Q3_1_4 after Median Filter')
imwrite(D,'Q3_1_4_Median.tif');

Part 2: restoring corrupted images using the Frequency Domain

צריך לעשות התמרת פורייה הפוכה מצגת 4ב שקופית 8

We will make Inverse Fourier Transform
  1. Restore the original images from the inputs
% צריך לחזור על התרגיל
I = im2double(imread('Q3_2_3.tif'));
imshow(I);
title('Original Image (courtesy of MIT)');
LEN = 5;
THETA = 135;
PSF = fspecial('motion', LEN, THETA);
blurred = imfilter(I, PSF, 'conv', 'circular');
figure, imshow(blurred)
noise_mean = 0;
noise_var = 0.0001;
blurred_noisy = imnoise(blurred, 'gaussian', ...
noise_mean, noise_var);
figure, imshow(blurred_noisy)
title('Simulate Blur and Noise')

Part 3: Enhancing low-quality image using high pass filter.

2. Implement the whole procedure listed on the page 43 of the lecture 4 (see also all the section on High Pass Filter). Submit your code and output image. The input image is Q3_4.tif
לנסות לשים מטריצה שמחדדת

השקף המדובר מתוך ההרצאה

I = imread("Q4_3.tif");
figure,imshow(I),title('The digital image section')
Now we will resizing the image to 50% of its original size
I1 = I(100:399,100:399);
figure,imshow(I1),title('Re-Sampling')
Now we will blurring the Original Image with 3 X 3 averaging filter
D=fspecial('average',[3,3]); %exact 3 X 3 average filter
d=imfilter(I,D);
figure,imshow(d);title('3 X 3 average filter',"FontSize",15);
figure,imshowpair(I,d,'montage')
title('Original Image vs Image after Averaging filter',"FontSize",15)
אופציה שנייה - לא בטוח שנכון
%blurredImage = imfilter(I, ones(3)/27, 'symmetric');
%figure,imshow(blurredImage);
%title('Blurred Image', 'FontSize', 15);

Part 4:

p = imread('Q4_2.tif');
figure,imshow(p),title('512x512 pixel image');
P = im2double(p);
לקחת תמונה להוסיף את הסינוס ואז לחזור עם התמרת פורייה
הסינוס זה רעש
Show your image.
x = rand(512:512);
y = rand(512:512);
z = sin(0.1.*x)+cos(0.4.*x)+sin(0.15*(x.^2+y.^2))+sin(0.35*(x.^2+y.^2));
w = P + z;
figure,imshow(w,[]),title('Image with noise')
figure,imshowpair(P,w,'montage')
title('Original vs Noisy Image',"FontSize",15)
Image_fft = fft2(w);
figure,imshow(log(abs(fftshift(Image_fft)+1)),[])
title('Magnitude of DFT')
figure, imshow(angle(fftshift(Image_fft)),[-pi pi])
title('Phase of DFT')
figure,imshow(2.*log(abs(fftshift(Image_fft)+1)),[])
title('Magnitude multiply by 2 of DFT')
Now We calculate the inverse fft
back_to_Image=ifft2(Image_fft);
Image_min = min(min(abs(back_to_Image)));
Image_max = max(max(abs(back_to_Image)));
figure, imshow(abs(back_to_Image), [Image_min Image_max])
title('Image after inverse Discrete Fourier Transform')
figure,imshowpair(w,back_to_Image,'montage')
title(' Step 2 Image vs IDFT Image',"FontSize",15)
Part 5:
לקחת תמונה ולבצע בה תהליך עם נוצ' פילטר
A band-stop filter is a filter that passes most frequencies, but attenuates those in a specific range to very low levels. A notch filter is a band-stop filter with a narrow stopband. These filters are using to remove periodic noise that can be approximated as two-dimensional sinusoidal functions from the image.
For example, in the above figure at left you can see the satellite image of Florida showing horizontal scan lines, and at right you can see the result of applying a notch filter.
Use similar images and try to develop your own band-stop filter to remove the noise. For this purpose, you should calculate the DFT of the image and remove a specific range of frequencies and then calculate the inverse DFT. You need to test different frequency intervals to find the best filter.
footBall=imread('noiseball.png');
imshow(footBall),title('Our Image')
Determine good padding for Fourier transform
PQ = paddedsize(size(footBall));
Create Notch filters corresponding to extra peaks in the Fourier transform
H1 = notch('btw', PQ(1), PQ(2), 10, 50, 100);
H2 = notch('btw', PQ(1), PQ(2), 10, 1, 400);
H3 = notch('btw', PQ(1), PQ(2), 10, 620, 100);
H4 = notch('btw', PQ(1), PQ(2), 10, 22, 414);
H5 = notch('btw', PQ(1), PQ(2), 10, 592, 414);
H6 = notch('btw', PQ(1), PQ(2), 10, 1, 114);
Calculate the discrete Fourier transform of the image
F=fft2(double(footBall),PQ(1),PQ(2));
Apply the notch filters to the Fourier spectrum of the image
FS_football = F.*H1.*H2.*H3.*H4.*H5.*H6;
convert the result to the spacial domain
F_football=real(ifft2(FS_football));
Crop the image to undo padding
F_football=F_football(1:size(footBall,1), 1:size(footBall,2));
Display the Fourier Spectrum
Move the origin of the transform to the center of the frequency rectangle
Fc=fftshift(F);
Fcf=fftshift(FS_football);
use abs to compute the magnitude and use log to brighten display
S1=log(1+abs(Fc));
S2=log(1+abs(Fcf));
figure, imshow(S1,[]),title('Fourier Spectrum of Image')
figure, imshow(S2,[]),title('Spectrum of image after Butterworth notch filters')
Display the blurred image
figure, imshow(F_football,[]),title('Image after notch filter')

הפונקציות שלנו שעזרו לפתרון התרגיל

function PQ = paddedsize(AB, CD, PARAM)
if nargin == 1
PQ = 2*AB;
elseif nargin == 2 & ~ischar(CD)
PQ = AB + CD - 1;
PQ = 2 * ceil(PQ / 2);
elseif nargin == 2
m = max(AB); % Maximum dimension.
% Find power-of-2 at least twice m.
P = 2^nextpow2(2*m);
PQ = [P, P];
elseif nargin == 3
m = max([AB CD]); %Maximum dimension.
P = 2^nextpow2(2*m);
PQ = [P, P];
else
error('Wrong number of inputs.')
end
end
function H = notch(type, M, N, D0, x, y, n)
if nargin == 6
n = 1; % Default value of n.
end
% Generate highpass filter.
Hlp = lpfilter(type, M, N, D0, n);
H = 1 - Hlp;
H = circshift(H, [y-1 x-1]);
end
function H = lpfilter(type, M, N, D0, n)
% computing the required distances.
[U, V] = dftuv(M, N);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
% Begin fiter computations.
switch type
case 'ideal'
H = double(D <=D0);
case 'btw'
if nargin == 4
n = 1;
end
H = 1./(1 + (D./D0).^(2*n));
case 'gaussian'
H = exp(-(D.^2)./(2*(D0^2)));
otherwise
error('Unknown filter type.')
end
end
function [U, V] = dftuv(M, N)
%DFTUV Computes meshgrid frequency matrices.
% Set up range of variables.
u = 0:(M-1);
v = 0:(N-1);
% Compute the indices for use in meshgrid
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
% Compute the meshgrid arrays
[V, U] = meshgrid(v, u);
end

How to write your project: